import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JDBC {
	private Connection conn;
	private String driverName = "com.mysql.jdbc.Driver";
	private String url;
	private String dataBaseUrl = "172.16.16.20";
	private String user = "root";
	private String password = "123456";
	private String dataBase;
	
	public static final String COL_NAME = "name";
	public static final String COL_TYPE = "type";
	public static final String COL_COMMENT = "comment";

	
	public JDBC(String dataBase) {
		this.dataBase = dataBase;
		this.url = "jdbc:mysql://"+dataBaseUrl+"/"+dataBase+"?characterEncoding=utf8";
		initConnection();
	}
	
	public void close() {
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException("关闭失败");
		}
	}
	
	// 初始化
    public void initConnection() {
        // 不同的数据库有不同的驱动
    	try {
        	System.out.println("加载驱动");
            Class.forName(driverName);
        	System.out.println("请求连接");
            this.conn = DriverManager.getConnection(url, user, password);
            System.out.println("数据库连接成功..");
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("获取连接初始化失败");
		}
    }
    
    /**
     * 获取字段集合
     * @author ye.yangyang
     * 2018年7月11日
     * @param tableName
     * @return
     * @throws SQLException 
     */
    public List<Map<String,String>> getColRet(String tableName) {
    	try {
        	System.out.println("获取字段集合");
            DatabaseMetaData databaseMetaData = conn.getMetaData();
            ResultSet colRet = databaseMetaData.getColumns(null,"%", tableName,"%"); 
        	List<Map<String,String>> rs = new ArrayList<Map<String,String>>();
            while(colRet.next()) {
            	Map<String,String> map = new HashMap<String, String>();
            	map.put("name", JavaTool.mysqlColNameTurnToJavaProName(colRet.getString("COLUMN_NAME")));
            	map.put("type", mysqlTypeTurnToJavaType(colRet.getString("TYPE_NAME")));
            	map.put("comment", colRet.getString("REMARKS"));
            	rs.add(map);
            }
            return rs;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("获取字段集合失败");
		}
    }
    
    
    public static void main(String[] args) {
    	String colName = "asd_qwe_zxc_yui";
    	colName = colName.toLowerCase();
    	char[] cs = colName.toCharArray();
    	boolean button = false;
    	for (int i = 0; i < cs.length; i++) {
    		if(cs[i]=='_') {
    			button=true;
    			continue;
    		}
    		if(button) {
    			if(cs[i]>='a'&&cs[i]<='z') {
    				cs[i] = (char)(cs[i]-32);
    			}
    			button = false;
    		}
		}
    	System.out.println(new String(cs).replaceAll("_", ""));
	}
    
    private String mysqlTypeTurnToJavaType(String mysqlType) {
    	String type = "";
    	switch (mysqlType) {
		case "BIGINT":
			type = "Integer";
			break;
		case "VARCHAR":
			type = "String";
			break;
		case "INT":
			type = "Integer";
			break;
		case "TIMESTAMP":
			type = "String";
			break;
		case "LONGTEXT":
			type = "String";
			break;
		case "DECIMAL":
			type = "Double";
			break;
		case "DATE":
			type = "Date";
			break;
		default:
			throw new RuntimeException("未知类型 需要定义:"+mysqlType);
		}
    	return type;
    }
}
